package com.mobilnaElka.activities.calendar;

import android.app.Activity;
import android.content.*;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

import com.fileChooser.FileChooser;
import com.mobilnaElka.AppConsts;
import com.mobilnaElka.activities.*;
import com.mobilnaElka.models.calendar.Consts;

public class CalendarSettingsActivity extends Activity {

	private FileChooser fileChooser;
	private SharedPreferences preferences;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		fileChooser = new FileChooser(savedInstanceState, this);
		preferences = getSharedPreferences(AppConsts.SETTINGS_PREF,
				Context.MODE_PRIVATE);

		initUI();

	}

	private void initUI() {
		TextView textView = new TextView(this);
		textView.setText(R.string.Settings);
		setContentView(R.layout.calendar_settings);

		LinearLayout chooseFile = (LinearLayout) findViewById(R.id.chooseFile);
		chooseFile.setOnClickListener(chooseFileOnClick);

		EditText editText = (EditText) findViewById(R.id.timetableURL);
		String textFromPreferences = preferences.getString(
				Consts.SETTINGS_FILE_NAME, "");
		editText.setText(textFromPreferences);

		final Button button1 = (Button) findViewById(R.id.importTimetableButton);
		button1.setOnClickListener(parsePlan);
		
	}

	private OnClickListener chooseFileOnClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			fileChooser.showDialog();

		}
	};

	private OnClickListener parsePlan = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent myIntent = new Intent(CalendarSettingsActivity.this,
					CalendarsListActivity.class);
			finish();
			startActivityForResult(myIntent, 0);

		}
	};
	
	@Override
	public void onBackPressed() {
		Intent myIntent = new Intent(CalendarSettingsActivity.this, MainActivity.class);
		finish();
		startActivityForResult(myIntent, 0);


	}
	
	
	public void setChosenFile(String chosenFilePath) {
		if (chosenFilePath != null) {
			final EditText editText = (EditText) findViewById(R.id.timetableURL);
			editText.setText(chosenFilePath);

			SharedPreferences.Editor preferencesEditor = preferences.edit();
			preferencesEditor.putString(Consts.SETTINGS_FILE_NAME, editText
					.getText().toString());
			preferencesEditor.putBoolean(Consts.SETTINGS_FILE_CHECKED, true);

			preferencesEditor.commit();
		}
	}

}